package com.huawei.hmsdemo10086.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.media.Image;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;


import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.huawei.hmsdemo10086.POJO.Goods;
import com.huawei.hmsdemo10086.R;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class GoodsDetailsActivity extends AppCompatActivity {


    private Goods goods;
    private BottomSheetDialog bottomSheetDialog;
    private BottomSheetBehavior mDialogBehavior;
    ImageView dialog_goods_img;
    TextView dialog_goods_name;
    TextView dialog_goods_price;
    Button btnDecrease;
    Button btnIncrease;
    Button submit;
    TextView etAmount;
    int num = 1;
    private SharedPreferences sharedPreferences;
    private String userId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_details);

        TextView mTitleTextView = (TextView) this.findViewById(R.id.text_title);
        mTitleTextView.setText("商品详情");
        Button back = (Button)this.findViewById(R.id.button_backward);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GoodsDetailsActivity.this,GoodsActivity.class);
                startActivity(intent);
                finish();
            }
        });

        sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        userId = sharedPreferences.getString("userId",null);

        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this);
        View view = getLayoutInflater().inflate(R.layout.dialog_bottomsheet_layout,null);
        dialog_goods_name = view.findViewById(R.id.diglog_goods_name);
        dialog_goods_price = view.findViewById(R.id.diglog_goods_price);
        dialog_goods_img = view.findViewById(R.id.diglog_goods_img);
        btnDecrease = view.findViewById(R.id.btnDecrease);
        btnIncrease = view.findViewById(R.id.btnIncrease);
        etAmount = view.findViewById(R.id.etAmount);
        submit = view.findViewById(R.id.diglog_submit);

        bottomSheetDialog.setContentView(view);

        goods = (Goods) getIntent().getSerializableExtra("goods");
        Log.d("goods", goods.toString());
        ImageView goods_img = findViewById(R.id.details_goods_img);
        TextView goods_price = findViewById(R.id.detail_goods_price);
        TextView goods_name = findViewById(R.id.detail_goods_name);
        Button buy_button = findViewById(R.id.detail_buy);
        Button buy_car = findViewById(R.id.buy_car_button);

        Glide.with(this).load(goods.getPicaddress()).into(goods_img);
        goods_name.setText(goods.getName());
        goods_price.setText("¥"+String.valueOf(goods.getPrice()));
        /*初始化dialog
         * */
        initDialog();


        buy_car.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GoodsDetailsActivity.this,CarActivity.class);
                startActivity(intent);
                finish();
            }
        });
        buy_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomSheetDialog.show();
            }
        });
    }
    private void initDialog(){
        Glide.with(this).load(goods.getPicaddress()).into(dialog_goods_img);
        dialog_goods_name.setText(goods.getName());
        dialog_goods_price.setText("¥"+String.valueOf(goods.getPrice()));
        btnDecrease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(num>1){
                    num--;
                    etAmount.setText(String.valueOf(num));
                }
            }
        });
        btnIncrease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                num++;
                etAmount.setText(String.valueOf(num));
            }
        });

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = "http://10.0.2.2:8443/api/goodsToCar";
                OkHttpClient mOkHttpClient = new OkHttpClient();

                String json = "{\n" +
                        "  \"userid\": \"" + userId + "\",\n" +
                        "  \"num\": \"" + num + "\",\n" +
                        "  \"goodsid\": \"" + goods.getGoodsid() + "\"\n" +
                        "}";
                Request request = new Request.Builder().url(url).post(RequestBody.create(MediaType.parse("application/json"),json)).build();

                mOkHttpClient.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e("测试", e+"");
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                    }
                });
//                Intent intent = new Intent(GoodsDetailsActivity.this,PayActivity.class);
//                intent.putExtra("goods",goods);
//                intent.putExtra("num",num);
//                Log.d("num", "Goods num:"+num);
//                startActivity(intent);
                Toast.makeText(GoodsDetailsActivity.this, "加入成功", Toast.LENGTH_SHORT).show();
            }
        });
    }



}